package com.hschinese.pronunciation.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.interfaces.LessonProgressInterface;
import com.hschinese.pronunciation.interfaces.NextLessonInterface;
import com.hschinese.pronunciation.interfaces.RecordStatusListener;
import com.hschinese.pronunciation.interfaces.VolumeListener;
import com.hschinese.pronunciation.utils.AnimatorUtil;
import com.hschinese.pronunciation.utils.Logger;
import com.hschinese.pronunciation.utils.NumberFormatUtil;
import com.hschinese.pronunciation.widget.ConsonantStudyView;
import com.hschinese.pronunciation.widget.DictationPracticeView;
import com.hschinese.pronunciation.widget.GestureListener;
import com.hschinese.pronunciation.widget.HsDialog;
import com.hschinese.pronunciation.widget.KnowledgePointView;
import com.hschinese.pronunciation.widget.MyPopupWindow;
import com.hschinese.pronunciation.widget.PractiseView;
import com.hschinese.pronunciation.widget.StudyWordsView;
import com.hschinese.pronunciation.widget.VowelStudyView;

import java.util.List;

import studychinese.model.TextRecultBean;
import studychinese.model.UserBean;


public class StudyActivity extends BaseActivity implements View.OnClickListener {
    private ImageView mExitView = null;
    private ProgressBar mProgressbar = null;
    private RelativeLayout mNextWordView = null;
    private RelativeLayout mGotoPractise = null;
    private LinearLayout mContainerView = null;
    private View mMainView;

    private StudyWordsView mStudyWordsView;
    private PractiseView mPractiseView;
    private VowelStudyView mVowelStudyView;
    private ConsonantStudyView mConsonantStudyView;
    private KnowledgePointView mKnowledgePointView;

    // 0,2学习，1,3 练习
    private int mCurrentLesson = 0;
    private int mCurrentPosition = 0;
    private NextLessonInterface mNextListener;
    private MainGestrueListener mGestureListener;
    private boolean isStarted = true;
    private RecordStatusListener mRecordListener;
    private VolumeListener mVolumeListener;
    private MyPopupWindow mPopupWindow;
    private TextView tv_process;
    private View baseView;
    private DictationPracticeView mDictationPracticeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study);
        Intent it = getIntent();
        // 启动出决定开始的是哪个课程
        mCurrentLesson = it.getIntExtra("lesson", 0);
        mCurrentPosition = it.getIntExtra("position", 0);
        mCurrentPosition = it.getIntExtra("position", 0);
        Logger.e("xionghy-currentLesson: " + mCurrentLesson);
        setNextListener();
        mGestureListener = new MainGestrueListener(StudyActivity.this);
        initViews();
        isStarted = false;
    }

    private void initViews() {
        mExitView = (ImageView)findViewById(R.id.study_quit);
        mExitView.setOnClickListener(this);
        mProgressbar = (ProgressBar)findViewById(R.id.study_progress);
        tv_process = (TextView) findViewById(R.id.tv_process);
        mNextWordView = (RelativeLayout)findViewById(R.id.study_next_btn);
        mNextWordView.setOnClickListener(this);
        mGotoPractise = (RelativeLayout)findViewById(R.id.study_goto_practise_btn);
        mGotoPractise.setOnClickListener(this);

        mMainView = findViewById(R.id.study_main_view);
        mMainView.setLongClickable(true);
        mMainView.setOnTouchListener(mGestureListener);
        baseView = findViewById(R.id.study_header_dividing_line);
        mPopupWindow = new MyPopupWindow(StudyActivity.this);

        initRecordListener();
        initVolumeListener();

        mContainerView = (LinearLayout)findViewById(R.id.study_container);
        if (mCurrentLesson == 1 || mCurrentLesson == 3) {
            initPractiseData();
        } else if(mCurrentLesson == 0 || mCurrentLesson == 2) {
            initStudyData();
        } else if (mCurrentLesson == 4) {
            initVowelData();
        } else if (mCurrentLesson == 5 || mCurrentLesson == 7) {
            initConsonantData();
        } else if (mCurrentLesson == 6) {
            initKnowledgeData();
        }else if (mCurrentLesson == 9 ||  mCurrentLesson==8 ) {
            initDactationPracticeData();
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mStudyWordsView != null) {
            mStudyWordsView.onDestroy();
        }
        if (mPractiseView != null) {
            mPractiseView.onDestroy();
        }
        if (mVowelStudyView != null) {
            mVowelStudyView.onDestroy();
        }
        if (mConsonantStudyView != null) {
            mConsonantStudyView.onDestroy();
        }
        if (mKnowledgePointView != null) {
            mKnowledgePointView.onDestroy();
        }if (mDictationPracticeView != null) {
            mDictationPracticeView.onDestroy();
        }
    }

    private void initStudyData() {
        mStudyWordsView = new StudyWordsView(StudyActivity.this);
        mStudyWordsView.setNextListener(mNextListener);
        mStudyWordsView.setLessonType(mCurrentLesson);
        mStudyWordsView.setVolumeListener(mVolumeListener);
        mStudyWordsView.setRecordListener(mRecordListener);
        mContainerView.addView(mStudyWordsView);
        mGotoPractise.setVisibility(View.VISIBLE);
        mStudyWordsView.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                Logger.e("setProgress -- total: " + total + " --  position:" + position);
                mProgressbar.setMax(total);
                mProgressbar.setProgress(position + 1);
                tv_process.setText( NumberFormatUtil.getPercent(position,total)+"%");
            }
        });
        mStudyWordsView.initData();
    }

    private void initPractiseData() {
        mPractiseView = new PractiseView(StudyActivity.this);
        mPractiseView.setNextListener(mNextListener);
        mPractiseView.setLessonType(mCurrentLesson);
        mContainerView.addView(mPractiseView);
        mGotoPractise.setVisibility(View.GONE);
        mPractiseView.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                Logger.e("setProgress -- total: " + total + " --  position:" + position);
                mProgressbar.setMax(total);
                mProgressbar.setProgress(position + 1);
                tv_process.setText( NumberFormatUtil.getPercent(position,total)+"%");
            }
        });
        mPractiseView.initData();
    }
    private void initDactationPracticeData() {
        clearData();
        mDictationPracticeView = new DictationPracticeView(StudyActivity.this);
        mDictationPracticeView.setNextListener(mNextListener);
        mDictationPracticeView.setLessonType(mCurrentLesson);
        mContainerView.addView(mDictationPracticeView);
        mGotoPractise.setVisibility(View.GONE);
        mDictationPracticeView.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                Logger.e("setProgress -- total: " + total + " --  position:" + position);
                mProgressbar.setMax(total);
                mProgressbar.setProgress(position + 1);
                tv_process.setText( NumberFormatUtil.getPercent(position,total)+"%");
            }
        });
        mDictationPracticeView.initData();
    }
    private void clearData() {
        TextRecultBean tetResultInfor = UserBean.getTetResultInfor();
          tetResultInfor.setState(0);
         List<TextRecultBean.DataBean> data = tetResultInfor.getData();
         data.clear();
          UserBean.putTetResultInfor(this,tetResultInfor);

    }
    private void initVowelData() {
        mVowelStudyView = new VowelStudyView(StudyActivity.this);
        mVowelStudyView.setNextListener(mNextListener);
        mVowelStudyView.setCurrentPosition(mCurrentPosition);
        mVowelStudyView.setVolumeListener(mVolumeListener);
        mVowelStudyView.setRecordListener(mRecordListener);
        mContainerView.addView(mVowelStudyView);
        mGotoPractise.setVisibility(View.GONE);
        mVowelStudyView.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                Logger.e("setProgress -- total: " + total + " --  position:" + position);
                mProgressbar.setMax(total);
                mProgressbar.setProgress(position + 1);
                tv_process.setText( NumberFormatUtil.getPercent(position,total)+"%");
            }
        });
        mVowelStudyView.initData();
    }

    private void initConsonantData() {
        mConsonantStudyView = new ConsonantStudyView(StudyActivity.this);
        mConsonantStudyView.setNextListener(mNextListener);
        mConsonantStudyView.setLessonType(mCurrentLesson);
        mConsonantStudyView.setCurrentPosition(mCurrentPosition);
        mConsonantStudyView.setRecordListener(mRecordListener);
        mConsonantStudyView.setVolumeListener(mVolumeListener);
        mContainerView.addView(mConsonantStudyView);
        mGotoPractise.setVisibility(View.GONE);
        mConsonantStudyView.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                mProgressbar.setMax(total);
                mProgressbar.setProgress(position + 1);
                tv_process.setText( NumberFormatUtil.getPercent(position,total)+"%");
            }
        });
        mConsonantStudyView.initData();
    }

    private void initKnowledgeData() {
        mKnowledgePointView = new KnowledgePointView(StudyActivity.this);
        mKnowledgePointView.setNextListener(mNextListener);
        mKnowledgePointView.setLessonType(mCurrentLesson);
        mKnowledgePointView.setVolumeListener(mVolumeListener);
        mKnowledgePointView.setRecordListener(mRecordListener);
        mContainerView.addView(mKnowledgePointView);
        mGotoPractise.setVisibility(View.GONE);
        mKnowledgePointView.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                Logger.e("setProgress -- total: " + total + " --  position:" + position);
                mProgressbar.setMax(total);
                mProgressbar.setProgress(position + 1);
                tv_process.setText( NumberFormatUtil.getPercent(position,total)+"%");
            }
        });
        mKnowledgePointView.initData();
    }

    private void setNextListener () {
        mNextListener = new NextLessonInterface() {
            @Override
            public void onLessonChanged(int currentLesson) {
                mCurrentLesson = currentLesson;
                if (mCurrentLesson <= 0) return;
                if (currentLesson == 2) {
                    if (mPractiseView != null) {
                        mPractiseView.onDestroy();
                        mContainerView.removeView(mPractiseView);
                    }
                    initStudyData();
                } else if (currentLesson == 1 || currentLesson == 3) {
                    if (mStudyWordsView != null) {
                        mStudyWordsView.onDestroy();
                        mContainerView.removeView(mStudyWordsView);

                    }
                    initPractiseData();
                }  else if (currentLesson ==9 || currentLesson==8) {
                    if (mDictationPracticeView != null) {
                        mDictationPracticeView.onDestroy();
                        mContainerView.removeView(mDictationPracticeView);
                    }
                    initDactationPracticeData();
                } else if (currentLesson == 7) {
                    if (mKnowledgePointView != null) {
                        mKnowledgePointView.onDestroy();
                        mContainerView.removeView(mKnowledgePointView);
                    }
                    initConsonantData();
                } else if (currentLesson == 4) {
                    Intent it = new Intent(StudyActivity.this, WordListActivity.class);
                    it.putExtra("lessonType", currentLesson);
                    startActivity(it);
                    finish();
                } else if (currentLesson == 5) {
                    Intent it = new Intent(StudyActivity.this, ConsListActivity.class);
                    it.putExtra("lessonType", currentLesson);
                    startActivity(it);
                    finish();
                }else if (currentLesson == 6) {
                    if (mConsonantStudyView != null) {
                        mConsonantStudyView.onDestroy();
                        mContainerView.removeView(mConsonantStudyView);
                    }
                    initKnowledgeData();
                } else if(currentLesson >= 7) {
                    // 结束当前Activity，进入关卡页
                    finish();
                }
            }
        };
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.study_quit) {
            this.finish();
        } else if (id == R.id.study_next_btn) {
            gotoNextWord();
        } else if (id == R.id.study_goto_practise_btn) {
            // TODO 到当前课程对应的练习（有View的切换）
            mCurrentLesson += 1; // 切换到下一个，（只有Page1的四个入库有这个对应关系）
            // 重新获取数据，并更新界面
            if (mCurrentLesson == 1 || mCurrentLesson == 3) {
                mContainerView.removeView(mStudyWordsView);
                initPractiseData();
            } // else if ()
        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
        if (mStudyWordsView != null) {
            mStudyWordsView.onDestroy();
        }
        if (mPractiseView != null) {
            mPractiseView.onDestroy();
        }
    }

    private void gotoNextWord() {
        if (mCurrentLesson == 0 || mCurrentLesson == 2) {
            mStudyWordsView.nextWord();
        } else if (mCurrentLesson == 1 || mCurrentLesson == 3 ) {
            mPractiseView.nextWord();
            AnimatorUtil.slideIn(mPractiseView, 500, 0, null);
        }else if ( mCurrentLesson == 9  || mCurrentLesson == 8  ) {
            mDictationPracticeView.nextWord();
            AnimatorUtil.slideIn(mDictationPracticeView, 500, 0, null);
        } else if (mCurrentLesson == 4) {
            if (mVowelStudyView != null) {
                mVowelStudyView.nextWord();
                AnimatorUtil.slideIn(mVowelStudyView, 500, 0, null);
            }
        } else if (mCurrentLesson == 5 || mCurrentLesson == 7) {
            if (mConsonantStudyView != null) {
                mConsonantStudyView.nextWord();
                AnimatorUtil.slideIn(mConsonantStudyView, 500, 0, null);
            }
        } else if (mCurrentLesson == 6) {
            mKnowledgePointView.nextWord();
            AnimatorUtil.slideIn(mKnowledgePointView, 500, 0, null);
        }
    }

    private void initRecordListener() {
        mRecordListener = new RecordStatusListener() {
            @Override
            public void startRecord() {
                Logger.e("xionghy - study activity start record");
                mPopupWindow.setVolumeText(0.f);
                mPopupWindow.showPopupWindow(baseView);
            }

            @Override
            public void stopRecord() {
                Logger.e("xionghy - study activity stop record");
                mPopupWindow.dismiss();
            }
        };
    }

    private HsDialog kdxfParseWaitDlg = null;
    private void showWaitDialog() {
        kdxfParseWaitDlg = new HsDialog(StudyActivity.this, R.style.pop_dialog, getResources().getString(R.string.login_dlg_msg), true, false);
    }

    private void initVolumeListener() {
        mVolumeListener = new VolumeListener() {
            @Override
            public void updateVolume(float volume) {
                if (mPopupWindow != null) {
                    mPopupWindow.setVolumeText(volume);
                }
            }
        };
    }

    class MainGestrueListener extends GestureListener {

        public MainGestrueListener(Context context) {
            super(context);
        }

        @Override
        public boolean left() {
            gotoNextWord();
            return true;
        }

        @Override
        public boolean right() {
            if (mCurrentLesson == 0 || mCurrentLesson == 2) {
                mStudyWordsView.beforeWord();
            } else if (mCurrentLesson == 1 || mCurrentLesson == 3) {
                mPractiseView.beforeWord();
                AnimatorUtil.slideOut(mPractiseView, 500, 0, null);
            } else if (mCurrentLesson == 4) {
                mVowelStudyView.beforeWord();
                AnimatorUtil.slideOut(mVowelStudyView, 500, 0, null);
            } else if (mCurrentLesson == 5 || mCurrentLesson == 7) {
                mConsonantStudyView.beforeWord();
                AnimatorUtil.slideOut(mConsonantStudyView, 500, 0, null);
            } else if (mCurrentLesson == 6) {
                mKnowledgePointView.beforeWord();
                AnimatorUtil.slideOut(mKnowledgePointView, 500, 0, null);
            }
            return true;
        }
    }


}
